var Tenant = function() {
    var datalist_url = $('.datalist_url').val();

    var _init = function() {
        var table = $('#data-table');

        table.dataTable({
            "processing": true,
            "ordering": false, // 禁止排序
            serverSide: true,// 开启服务器模式
            "ajax": {
            	url: datalist_url,
            },
            "autoWidth": false,
            "columns": [
                {"data": "orderNo"},
                {"data": "transaction_id"},
                {
                    "data": "payMethod",
                    "render": function(data, type, row, meta) {
                        switch (data) {
                            case 0: data = '微信'; break;
                            case 1: data = '支付宝'; break;
                            case 2: data = '网银'; break;
                            default : break;
                        }
                        return data;
                    }
                },
                {
                    "data": "total",
                    "render": function(data, type, row, meta) {
                        return data / 100;
                    }
                },
                {
                    "data": "type",
                    "render": function(data, type, row, meta) {
                        switch (data) {
                            case 1: data = '平台消费'; break;
                            case 2: data = '达达同城配送'; break;
                            default : break;
                        }
                        return data;
                    }
                },
                {
                    "data": "createDate",
                    "render": function(data, type, row, meta) {
                        return moment(data).format('YYYY-MM-DD HH:mm');
                    }
                },
            ],
            "language": {
            	url: '/public/static/assets/pages/Chinese.json'
            },
            "lengthMenu": [[10, 20, 50, 100, 150], [10, 20, 50, 100, 150]],
            "pageLength": 10,
            "scrollX":"",
            "destroy": false,
            "stateSave": true,
            "stateSaveParams": function (settings, data) {
                var search = data.search.search;
                if(search){
                    var arr = $.parseJSON(search);
                    for(var key in arr){
                        $("#searchForm input[name='"+key+"']").val(arr[key]);
                        $("#searchForm select[name='"+key+"']").val(arr[key]);
                    }
                }
            },
            "columnDefs": [{
                "orderable": false,
            }],
            "dom": "<'row'<'.col-md-6 col-sm-12'><'col-md-6 col-sm-12'>r>" +
                "<t>" +
                "<'table_b relative'<'col-md-5'i><'col-md-7'>lp>",
            "fnInitComplete": function() {
            	var nTrs = table.fnGetNodes();//fnGetNodes获取表格所有行，nTrs[i]表示第i行tr对象
            }
        });
    };

    return {
        init: function() {
            _init();
        }
    };

}();


$(function() {
    Tenant.init();

    $("#buybtn").click(function () {
        var _data = $("#smsForm").serializeObject();
        var wxPay = 0;
        var zhifubao = 1;
        var wangyin = 2;

        //调用微信支付api
        //金额
        var total = $("#blance").val();
        if(!checkmoney(_data.blance)){
            $("#blance").focus();
            layer.msg("充值金额不合法",{icon: 5,time:2000});
            return false;
        }
        var payMethod = wxPay;
        var url = "/admin/TenantWxPay/tenantRecharge";
        var $param = {
            total:_data.blance,
            serviceType:_data.serviceType,
            payMethod:payMethod
        };
        $.ajax({
            type: 'POST',
            url: url,
            data:$param,
            dataType: 'json',
            success: function(data){
                //alert(data.data)
                if(data.code == 0){
                    layer.msg(data.message,{icon: 5,time:2000});
                }else {
                    var wxpayurl = data.data.url;
                    $("#wximg").attr("src",wxpayurl);
                }
            },
            error:function(data) {
                console.log(data.msg);
            },
        });
    });
});

function onlyNumber(obj){
    // 得到第一个字符是否为负号
    var t = obj.value.charAt(0);
    // 先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d\.]/g,'');
    // 必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g,'');
    // 保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g,'.');
    // 保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace('.','$#$').replace(/\./g,'').replace('$#$','.');
    // 如果第一位是负号，则允许添加
    if(t == '-'){
        obj.value = '-'+obj.value;
    }
}

function checkmoney($val) {
    if($val <= 0){
        return false;
    }
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(reg.test($val)){
        return true;
    }else {
        return false;
    }
}
